<?php

namespace App\Exports;

use App\Enums\ProductType;
use App\Models\Download;
use App\Models\Order;
use Illuminate\Support\Arr;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class ResaleExport implements FromQuery, WithMapping, WithHeadings
{
    public $data;
    public $request;

    public function __construct(Download $data)
    {
        $this->data = $data;
        $this->request = json_decode($this->data->request, true);
    }

    public function query()
    {
        $type = ProductType::RESALE();

        $query = Order::query()
            ->with(['user', 'payment'])
            ->when(!empty($this->request['keyword']), function ($q) {
                return $q->where(function ($q2) {
                    $likeVal = '%' . $this->request['keyword'] . '%';
                    $q2->where('order_no', 'like', $likeVal)
                        ->orWhere('id', $this->request['keyword'])
                        ->orWhere('product_name', 'like', $likeVal)
                        ->orWhereHas('user', function ($q1) use ($likeVal) {
                            return $q1->where('mobile', 'like', $likeVal);
                        });
                });
            })
            ->when(!empty($this->request['order_status']), function ($q) {
                $orderStatusArr = explode(',', $this->request['order_status']);
                $q->whereIn('order_status', $orderStatusArr);
            })
            ->when(!empty($this->request['trans_status']), function ($q) {
                $transStatusArr = explode(',', $this->request['trans_status']);
                $q->whereIn('trans_status', $transStatusArr);
            })
            ->when(!empty($this->request['divide_status']), function ($q) {
                $q->whereHas('payment', function ($q2) {
                    $divideStatusArr = explode(',', $this->request['divide_status']);
                    $q2->whereIn('divide_status', $divideStatusArr);
                });
            })
            ->whereHas('product', function ($q) use ($type) {
                return $q->where('type', $type->value)
                    ->when(!empty($this->request['resale_user']), function ($q2) {
                        $resaleUsereVal = '%' . $this->request['resale_user'] . '%';
                        $q2->whereHas('resale_user', function ($q1) use ($resaleUsereVal) {
                            return $q1->where('mobile', 'like', $resaleUsereVal);
                        });
                    });
            })
            ->when(!empty($this->request['start_date']), function ($q) {
                $q->whereDate('created_at', '>=', $this->request['start_date']);
            })
            ->when(!empty($this->request['end_date']), function ($q) {
                $q->whereDate('created_at', '<=', $this->request['end_date']);
            })->with(['product.resale_user']);
        if (!empty($this->request['agent_id'])) {
            $query = $query->where('agent_id', $this->request['agent_id']);
        }

        if (!empty($this->data['agent_id'])) {
            $query = $query->where('agent_id', $this->data['agent_id']);
        }

        return $query->orderByDesc('id');
    }

    public function map($value): array // 指定excel中每一列的数据字段
    {
        $orderStatus = Arr::get($value, 'order_status');
        $transStatus = Arr::get($value, 'trans_status');
        $divideStatus = Arr::get($value, 'payment.divide_status');

        return [
            Arr::get($value, 'id'),
            Arr::get($value, 'order_no'),
            Arr::get($value, 'product_name'),
            ' ' . Arr::get($value, 'product.resale_user.mobile', ''),
            ' ' . Arr::get($value, 'user.mobile', ''),
            Arr::get($value, 'display_order_amount'),
            Arr::get($value, 'created_at'),
            $orderStatus ? $orderStatus->description : '',
            $transStatus ? $transStatus->description : '',
            $divideStatus ? $divideStatus->description : '',
        ];
    }

    public function headings(): array // 指定excel的表头
    {
        return [
            'ID',
            '订单号',
            '产品名称',
            '出售方',
            '购买人',
            '金额',
            '下单时间',
            '订单状态',
            '资产状态',
            '分账状态',
        ];
    }
}
